package com.example.skillcinema.extensions

import com.example.skillcinema.db.repositories.BaseRepository.Companion.PREVIEW_ITEMS_COUNT

fun <T> List<T>.sliceForPreview(): List<T> {
    return if (this.count() > PREVIEW_ITEMS_COUNT) this.slice(0..<PREVIEW_ITEMS_COUNT)
    else this
}

fun <T> List<T>.sliceToPages(page: Int, pageSize: Int = PREVIEW_ITEMS_COUNT): List<T> {
    if (page <= 0 || pageSize <= 0) return emptyList()

    val fromIndex = (page - 1) * pageSize
    val toIndex = (page * pageSize).coerceAtMost(this.size)

    return if (fromIndex < this.size) this.subList(fromIndex, toIndex) else emptyList()
}